package com.dormitory123.millet.web;

import com.dormitory123.millet.pojo.Cart;
import com.dormitory123.millet.pojo.CartItem;
import com.dormitory123.millet.pojo.Phone;
import com.dormitory123.millet.service.Impl.PhoneServiceImpl;
import com.dormitory123.millet.service.PhoneService;
import com.dormitory123.millet.util.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yanhuanzhan
 * @date 2022/1/4 - 17:45
 */
public class CartServlet extends BaseServlet{
    private PhoneService phoneService = new PhoneServiceImpl();
    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
        Phone phone = phoneService.queryPhoneById(id);
        BigDecimal price = phone.getDiscountPrice().compareTo(BigDecimal.ZERO)==0 ? phone.getPrice() : phone.getDiscountPrice();
        System.out.println(phone.getDiscountPrice().equals(BigDecimal.ZERO));
        //将phone对象转换为CarItem对象
        CartItem cartItem = new CartItem(phone.getId(), phone.getName(), price,1, price,phone.getImgPath());
        System.out.println(cartItem.getPrice());
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        //保存最后一次添加到购物车的商品项
        req.getSession().setAttribute("lastItem",cartItem);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalCount", cart.getTotalCount());
        resultMap.put("cart", cart);
        Gson gson = new Gson();
        String toJson = gson.toJson(resultMap);
        resp.getWriter().write(toJson);
    }
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
        Phone phone = phoneService.queryPhoneById(id);
        BigDecimal price = phone.getDiscountPrice().equals(new BigDecimal(0)) ? phone.getPrice() : phone.getDiscountPrice();
        //将phone对象转换为CarItem对象
        CartItem cartItem = new CartItem(phone.getId(), phone.getName(), phone.getPrice(),1, phone.getPrice(),phone.getImgPath());
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        //保存最后一次添加到购物车的商品项
        req.getSession().setAttribute("lastItem",cartItem);
        resp.sendRedirect(req.getHeader("Referer"));
    }
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
         cart.deleteItem(id);
            System.out.println("deleteItem");
        }
        resp.sendRedirect(req.getHeader("Referer"));
    }
    //清空购物车
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clear();
        }
        resp.sendRedirect(req.getHeader("Referer"));
    }
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        String count=req.getParameter("count");
        Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
        if (cart != null) {
            if (count.equals("add")) {
                cart.updateCount(id,1);
            }else {
                cart.updateCount(id,-1);
            }
        }
        resp.sendRedirect(req.getHeader("Referer"));
    }
}
